/*
 * EmployeeSearchServlet.java
 * Created on 25. juni 2008, 12:11
 */
package webshop.del;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import webshop.data.del.Employee;



/**
 * EmployeeSearchServlet
 * @author even
 */
public class EmployeeSearchServlet extends AbstractDepartmentServlet {

    
    /**
     * Handle requests. Sends a html page containing the department list.
     *
     * @param req the request object
     * @param resp the response object
     *
     * @throws IOException
     * @throws ServletException
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        EntityManager em = emf.createEntityManager();
        PrintWriter out = response.getWriter();
        
        String name = request.getParameter("name");
        
        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache");
        response.setHeader("Expires", "0");

        out.println("<html><head><title>"+this.getClass().getName()+"</title></head><body>");
        out.println("<h1>Employee Search Results</h1>");
        out.println("<table>");
        Query q = em.createNamedQuery("Employee.findByName");
        q.setParameter("name", name+"%");
        List<Employee> emps = q.getResultList();
        for (Employee emp : emps) {
            out.println("<tr><td><a href=\"empdetail?empno="+emp.getEmpNo()+"\">"+emp.getName()+"</a></td></tr>");
        }
        out.println("</table>");

        out.println("<hr/>");
        out.println("<p><a href=\"index.html\">Home</a> &gt; <a href=\"deplist\">Department List</a></p>");
        out.println("</body>");
        out.println("</html>");
        
        em.close();
    }
}
